package applet.admin.components.events;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import applet.misc.component.JIntTextField;
import applet.misc.document.EnableDocumentListener;
import applet.misc.item.EnableItemListener;

import data.CMSRes;
import data.event.Event;
import data.event.EventTable;

public class EventEditDialog extends JDialog
{
	private static final long serialVersionUID = 6909971182869135332L;
	
	private Event event;
	private EventTable eventTable;

	private JTextField nameField;
	private JComboBox locationField;
	private JTextField dateField;
	private JIntTextField startTimeField;
	private JIntTextField endTimeField;
	private JTextArea descriptionField;

	private JButton saveButton;
	private JButton resetButton;
	
	public EventEditDialog(Event event, EventTable eventTable)
	{
		super(CMSRes.frame(), "Edit Event", true);
		this.event = event;
		this.eventTable = eventTable;
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		
		defineComponents();
		setLayout(new BorderLayout());
		add(getInfoPanel(), BorderLayout.NORTH);
		add(getDescriptionPanel(), BorderLayout.CENTER);
		add(getButtonBar(), BorderLayout.SOUTH);
		pack();
	}
	
	private void defineComponents()
	{
		saveButton = new JButton("Save");
		saveButton.setFont(saveButton.getFont().deriveFont(Font.BOLD));
		saveButton.setPreferredSize(new Dimension(200, 30));
		saveButton.setEnabled(false);
		saveButton.addActionListener(new SaveListener());
		
		resetButton = new JButton("Reset");
		resetButton.setFont(resetButton.getFont().deriveFont(Font.BOLD));
		resetButton.setPreferredSize(new Dimension(200, 30));
		resetButton.setEnabled(false);
		resetButton.addActionListener(new ResetListener());
		
		nameField = new JTextField(event.name());
		nameField.setPreferredSize(new Dimension(150, 30));
		nameField.getDocument().addDocumentListener(new EnableDocumentListener(resetButton, true));
		nameField.getDocument().addDocumentListener(new EnableDocumentListener(saveButton, true));
		
		dateField = new JTextField(event.date());
		dateField.setPreferredSize(new Dimension(150, 30));
		dateField.getDocument().addDocumentListener(new EnableDocumentListener(resetButton, true));
		dateField.getDocument().addDocumentListener(new EnableDocumentListener(saveButton, true));
		
		String[] locations = CMSRes.conference().locations().toArray(new String[0]);
		locationField = new JComboBox(locations);
		for(int i = 0; i < locationField.getItemCount(); i++)
			if(locationField.getItemAt(i).equals(event.location()))
				locationField.setSelectedIndex(i);
		locationField.addItemListener(new EnableItemListener(resetButton, true));
		locationField.addItemListener(new EnableItemListener(saveButton, true));
		
		descriptionField = new JTextArea(event.description());
		descriptionField.getDocument().addDocumentListener(new EnableDocumentListener(resetButton, true));
		descriptionField.getDocument().addDocumentListener(new EnableDocumentListener(saveButton, true));
		descriptionField.setWrapStyleWord(true);
		descriptionField.setLineWrap(true);
		
		startTimeField = new JIntTextField(event.startTime());
		startTimeField.setPreferredSize(new Dimension(150, 30));
		startTimeField.getDocument().addDocumentListener(new EnableDocumentListener(resetButton, true));
		startTimeField.getDocument().addDocumentListener(new EnableDocumentListener(saveButton, true));
		
		endTimeField = new JIntTextField(event.endTime());
		endTimeField.setPreferredSize(new Dimension(150, 30));
		endTimeField.getDocument().addDocumentListener(new EnableDocumentListener(resetButton, true));
		endTimeField.getDocument().addDocumentListener(new EnableDocumentListener(saveButton, true));
	}
	
	private JPanel getInfoPanel()
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(5, 5, 5, 5));
		
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(5, 5, 5, 5));
				JLabel labelHelper = new JLabel("Event Info:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper, BorderLayout.NORTH);
			layoutHelper = new JPanel(new GridLayout(0, 2));
			
				JPanel layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(10, 10, 10, 3));
					labelHelper = new JLabel("Name:");
					labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layout2.add(labelHelper, BorderLayout.EAST);
			layoutHelper.add(layout2);
				
				layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(10, 3, 10, 10));
				layout2.add(nameField, BorderLayout.WEST);
			layoutHelper.add(layout2);
	
				layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(10, 10, 10, 3));
					labelHelper = new JLabel("Date:");
					labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layout2.add(labelHelper, BorderLayout.EAST);
			layoutHelper.add(layout2);
				
				layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(10, 3, 10, 10));
				layout2.add(dateField, BorderLayout.WEST);
			layoutHelper.add(layout2);
				
				layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(10, 10, 10, 3));
					labelHelper = new JLabel("Location:");
					labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layout2.add(labelHelper, BorderLayout.EAST);
			layoutHelper.add(layout2);
				
				layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(10, 3, 10, 10));
				layout2.add(locationField, BorderLayout.WEST);
			layoutHelper.add(layout2);
				
				layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(10, 10, 10, 3));
					labelHelper = new JLabel("Start Time:");
					labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layout2.add(labelHelper, BorderLayout.EAST);
			layoutHelper.add(layout2);
			
				layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(10, 3, 10, 10));
				layout2.add(startTimeField, BorderLayout.WEST);
			layoutHelper.add(layout2);
	
				layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(10, 10, 10, 3));
					labelHelper = new JLabel("End Time:");
					labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
				layout2.add(labelHelper, BorderLayout.EAST);
			layoutHelper.add(layout2);
			
				layout2 = new JPanel(new BorderLayout());
				layout2.setBorder(new EmptyBorder(10, 3, 10, 10));
				layout2.add(endTimeField, BorderLayout.WEST);
			layoutHelper.add(layout2);
					
		panel.add(layoutHelper, BorderLayout.CENTER);
		
		return panel;
	}
	
	private JPanel getDescriptionPanel()
	{
		JPanel panel = new JPanel(new BorderLayout());
		panel.setBorder(new EmptyBorder(5, 5, 5, 5));
		
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(5, 5, 5, 5));
				JLabel labelHelper = new JLabel("Description:");
				labelHelper.setFont(labelHelper.getFont().deriveFont(Font.BOLD));
			layoutHelper.add(labelHelper, BorderLayout.WEST);
		panel.add(layoutHelper, BorderLayout.NORTH);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setPreferredSize(new Dimension(150, 150));
			layoutHelper.setBorder(new EmptyBorder(5, 5, 5, 5));
			layoutHelper.add(new JScrollPane(descriptionField), BorderLayout.CENTER);
		panel.add(layoutHelper, BorderLayout.CENTER);
		
		return panel;
	}
	
	private JPanel getButtonBar()
	{
		JPanel panel = new JPanel(new GridLayout(1, 0));
			JPanel layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(5, 10, 10, 5));
			layoutHelper.add(resetButton, BorderLayout.CENTER);
		panel.add(layoutHelper);
			layoutHelper = new JPanel(new BorderLayout());
			layoutHelper.setBorder(new EmptyBorder(5, 5, 10, 10));
			layoutHelper.add(saveButton, BorderLayout.CENTER);
		panel.add(layoutHelper);
		return panel;
	}
	
	private class ResetListener implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			saveButton.setEnabled(false);
			resetButton.setEnabled(false);
			
			nameField.setText(event.name());
			for(int i = 0; i < locationField.getItemCount(); i++)
				if(locationField.getItemAt(i).equals(event.location()))
					locationField.setSelectedIndex(i);
			descriptionField.setText(event.description());
			startTimeField.setText("" + event.startTime());
			endTimeField.setText("" + event.endTime());
		} }
	
	private class SaveListener implements ActionListener {
		@Override public void actionPerformed(ActionEvent e) {
			saveButton.setEnabled(false);
			resetButton.setEnabled(false);
			
			event.setName(nameField.getText());
			event.setLocation((String) locationField.getSelectedItem());
			event.setDescription(descriptionField.getText());
			event.setStartTime(startTimeField.getValue());
			event.setEndTime(endTimeField.getValue());
			
			eventTable.updateContents(eventTable.getSelectedRow(), eventTable.getSelectedColumn());
		} }
}